<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">代码生成器</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>代码生成器</cite></a>
        </span>
    </div>
    <div class="layui-card-body">

        <div class="layui-form toolbar">
            搜索：<input id="generator-edit-search" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="generator-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <table class="layui-table" id="generator-table" lay-filter="generator-table"></table>

    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="generator-table-bar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="generator">生成</a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#generator-table',
            url: config.base_server + 'api-generator/generator/list',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'engine', sort: true, title: '数据库引擎'},
                {field: 'tableName', sort: true, title: '表名'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', width:80, toolbar: '#generator-table-bar', title: '操作'}
            ]]
        });

        // 工具条点击事件
        table.on('tool(generator-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'generator') { // 删除
                generator(obj);
            }
        });

        // 搜索按钮点击事件
        $('#generator-btn-search').click(function () {
            var key = $('#generator-edit-search').val();
            table.reload('generator-table', {where: {tableName: key}});
        });

        // 生成
        var generator = function (obj) {
            // layer.msg("演示阶段", {icon: 1});
            var url = config.base_server + 'api-generator/generator/code?access_token='+config.getToken().access_token+"&tables="+ obj.data.tableName;
            window.location.href =  url;

            // var xhr = new XMLHttpRequest();
            // xhr.open('GET', url, true);
            // xhr.responseType = "blob";
            // xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            // xhr.onload = function() {
            //     if (this.status == 200) {
            //         var blob = this.response;
            //         var objecturl = URL.createObjectURL(blob);
            //         window.location.href = objecturl;
            //     }
            // }
            // xhr.send();

        };



    });
</script>